package com.spark.sniffer.plugin.plugins.define.trace;

import static net.bytebuddy.matcher.ElementMatchers.named;

import com.spark.sniffer.plugin.bytebuddy.ArgumentTypeNameMatch;
import com.spark.sniffer.plugin.interceptor.InstanceMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.StaticMethodsInterceptPoint;
import com.spark.sniffer.plugin.interceptor.enhance.ClassInstanceMethodsEnhancePluginDefine;
import com.spark.sniffer.plugin.match.ClassMatch;
import com.spark.sniffer.plugin.match.NameMatch;
import com.spark.sniffer.plugin.plugins.interceptor.LogbackInterceptor;

import net.bytebuddy.description.method.MethodDescription;
import net.bytebuddy.matcher.ElementMatcher;

public class LogbackPlugin extends ClassInstanceMethodsEnhancePluginDefine {

    @Override
    protected ClassMatch enhanceClass() {
        return NameMatch.byName("ch.qos.logback.classic.pattern.MDCConverter");
    }

    @Override
    protected InstanceMethodsInterceptPoint[] getInstanceMethodsInterceptPoints() {
        return new InstanceMethodsInterceptPoint[] {new InstanceMethodsInterceptPoint() {
            @Override
            public ElementMatcher<MethodDescription> getMethodsMatcher() {
                return named("convert")
                    .and(ArgumentTypeNameMatch.takesArgumentWithType(0, "ch.qos.logback.classic.spi.ILoggingEvent"));
            }

            @Override
            public Class<?> getMethodsInterceptor() {
                return LogbackInterceptor.class;
            }

            @Override
            public boolean isOverrideArgs() {
                return false;
            }
        }};
    }

    @Override
    protected StaticMethodsInterceptPoint[] getStaticMethodsInterceptPoints() {
        return null;
    }

}
